package com.ljwy.showinfo;

import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.ljwy.showinfo.info.MsgInfo;
import com.ljwy.showinfo.okgoHelper.GsonConvert;
import com.ljwy.showinfo.okgoHelper.HttpRequestHelper;
import com.ljwy.showinfo.okgoHelper.JsonCallback;
import com.ljwy.showinfo.utils.Constant;
import com.ljwy.showinfo.utils.NfcUtils;
import com.ljwy.showinfo.utils.ToastUtils;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.info_tv)
    TextView infoTv;
    private NfcUtils nfcUtils;
    private SoundPool soundPool;
    private HashMap<String, String> hashMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        nfcUtils = new NfcUtils(this);
        soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 0);
        soundPool.load(mContext, R.raw.duka3, 1);
        hashMap = new HashMap<>();
    }


    @Override
    protected void onResume() {
        super.onResume();
        nfcUtils.enableForegroundDispatch();
        soundPool.autoResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        nfcUtils.disableForegroundDispatch();
        soundPool.autoPause();
    }

    private long firstTime;

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {
            ToastUtils.showShort("再按一次退出程序");
            firstTime = secondTime;
        } else {
            finish();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String id = nfcUtils.readNFCId(nfcUtils.getNFCTag(intent));
        if (TextUtils.isEmpty(id)) {
            ToastUtils.showShort("nfc读取失败");
        } else {
            soundPool.play(1, 1, 1, 1, 0, 1);
            hashMap.clear();
            hashMap.put("terminal_no", id);
            HttpRequestHelper.getRequest(mContext, Constant.GET_INFO, hashMap, new JsonCallback<String>() {
                @Override
                public void onSuccess(Response<String> response) {
                    MsgInfo info = GsonConvert.fromJson(response.body(), MsgInfo.class);
                    if (info.getData() == null) {
                        infoTv.setText("暂无此人信息");
                    } else {
                        MsgInfo.DataBean data = info.getData();
                        infoTv.setText("赛事名:" + data.getActivity_name() + "\n" + "姓名:" + data.getName() + "\n" + "号码簿:" + data.getNo() + "\n" + "组别:" + data.getPackage_name());
                    }
                }
            });
        }
    }
}
